import java.io.IOException;

import processing.core.PApplet;
import processing.core.PImage;


public class Square extends Node {
	float rotation;
	float width = 10f;
	float heigth = 10f;
	String userName;
	float[] center;
	float radius;
	float toTurn = 0f;
	Circle song;
	float extraRadius = 100f;	 
	boolean highlight = false;
	PImage image;
	float imageOffset;
	
	public Square(PApplet sketch, float[] center, float radius, float rotation, String userName, String imageURL, float offset, Circle song){
		this.sketch = sketch;
		this.center = center.clone();
		this.rotation = rotation - (float)Math.PI/2;
		this.radius = radius;
		this.mass = 100f;
		this.userName = userName;
		if (imageURL != null){
			try{
				this.image = sketch.loadImage(imageURL);
			}
			catch(Exception e){
//				e.printStackTrace();
				this.image = null;
			}
		}
		else
			this.image = null;
		this.imageOffset = offset;
		this.song = song;
		updatePosition();
	}
	
	/**
	 * opdates the position according to it's rotation and radius and center
	 */
	private void updatePosition(){
		position[0] = (float) Math.sin(rotation + Math.PI/2) * (radius + extraRadius) + center[0];
		position[1] = (float) -Math.cos(rotation + Math.PI/2) * (radius + extraRadius) + center[1];
	}
	
	/**
	 * gives the square order to turn the given angle. actual turning doesn't happen here
	 * @param angle
	 */
	public void turn(float angle){
		this.toTurn += angle;
	}
	
	/**
	 * do the actual rotating that was ordered in turn()
	 */
	private void rotate(){		
		if (toTurn > 0f){
			float angle; //the angle to rotate by
			float step = 0.005f;
			if (toTurn > step){
				toTurn -= step;
				angle = step;
			}
			else{
				angle = toTurn;
				toTurn = 0f;
			}
			rotation += angle;
		}
	}

	@Override
	public void exertForce(float[] force) {
		// TODO Auto-generated method stub

	}	

	@Override
	public float move() {
		// TODO Auto-generated method stub
		rotate();
		decreaseExtraRadius();
		updatePosition();
		return 0f;
	}

	private void decreaseExtraRadius() {
		if (extraRadius > 0f){
			float dist; //the dist to rotate by
			float step = 3f;
			if (extraRadius > step){
				extraRadius -= step;			
			}
			else{			
				extraRadius = 0f;
			}			
		}
	}

	@Override
	public void dampen() {
		// TODO Auto-generated method stub
		
	}

	public Circle getSong() {
		// TODO Auto-generated method stub
		return song;
	}

}
